package fnord.jovian.tango;

import java.util.Vector;

import fnord.jovian.*;
import fnord.jovian.tango.TheAssemblyLayout.TheAssemblyOrientation;

public class TheSettingsMenu extends TheMenu {

	private boolean _active;
	private Vector<TheTextField> _fields;
	
	public TheSettingsMenu(TheBoard board) {
		super(300,400,board);
		_fields = new Vector<TheTextField>();
		TheAssemblyLayout layout = new TheAssemblyLayout(TheAssemblyOrientation.VERTICAL);
		TheAssemblyLayout horzLayout = new TheAssemblyLayout(TheAssemblyOrientation.HORIZONTAL, 0);
		setTheLayout(layout);
		ThePanel bigPanel = new ThePanel(0,0,280,250,board);
		bigPanel.setTheLayout(layout);
		
		ThePanel players = new ThePanel(0,0,250,30,board);
		players.showBackground(false);
		players.setTheLayout(horzLayout);
		players.addComponent(new TheText("Number of Players: ",10,200,30,true,false));
		TheTextField playerField = new TheTextField(board,40,20);
		playerField.setNumbersOnly(true, 2);
		playerField.setValue(String.valueOf(board.getCommunicator().getPlayers()));
		players.addComponent(playerField);
		bigPanel.addComponent(players);
		_fields.add(playerField);
		
		ThePanel address = new ThePanel(0,0,250,30,board);
		address.showBackground(false);
		address.setTheLayout(horzLayout);
		address.addComponent(new TheText("Address: ",10,60,30,true,false));
		TheTextField addressField = new TheTextField(board,150,20);
		addressField.setValue(String.valueOf(board.getCommunicator().getAddress()));
		address.addComponent(addressField);
		bigPanel.addComponent(address);
		_fields.add(addressField);
		
		ThePanel port = new ThePanel(0,0,250,30,board);
		port.showBackground(false);
		port.setTheLayout(horzLayout);
		port.addComponent(new TheText("Port: ",10,200,30,true,false));
		TheTextField portField = new TheTextField(board,40,20);
		portField.setNumbersOnly(true, 4);
		portField.setValue(String.valueOf(board.getCommunicator().getPort()));
		port.addComponent(portField);
		bigPanel.addComponent(port);
		_fields.add(portField);
			
		ThePanel buttons = new ThePanel(0,0,250,30,board);
		buttons.showBackground(false);
		buttons.setTheLayout(horzLayout);
		buttons.addComponent(new TheButton("Apply", 110, 30, new TheApplyListener()));
		buttons.addComponent(new TheButton("Cancel", 110, 30, new TheCancelListener()));
		this.addComponent(bigPanel);
		this.addComponent(buttons);
	}

	private class TheApplyListener implements TheListener {
		public void doAction() {
			_board.getCommunicator().setPlayers(Integer.parseInt(_fields.get(0).getValue()));
			_board.getCommunicator().setAddress(_fields.get(1).getValue());
			_board.getCommunicator().setPort(Integer.parseInt(_fields.get(2).getValue()));
			_board.toggleSettings();
		}
	}

	private class TheCancelListener implements TheListener {
		public void doAction() {
			_board.toggleSettings();
		}
	}
	
	public void setActive(boolean active) {
		_active = active;
		for (int i = 0; i < _fields.size(); i++) {
			_fields.get(i).setActive(active);
		}
	}
	
	public boolean getActive() {
		return _active;
	}

}
